# 2.14 基本的绘制函数
# 绘制图形：矩形、椭圆形、线条、单个的像素
import pygame, sys
from pygame.constants import *

pygame.init()

displaysurf = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("Drawing")

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

# 填充整个Surface对象
displaysurf.fill(white)

# 绘制多边形，points是由一个或多个点组成的列表
pygame.draw.polygon(
    displaysurf, green, ((146, 0), (291, 106), (236, 277), (56, 277), (0, 106))
)

# 绘制一条直线，最后一个参数是线宽
pygame.draw.line(displaysurf, blue, (60, 60), (120, 60), 4)
pygame.draw.line(displaysurf, blue, (120, 60), (60, 120))
pygame.draw.line(displaysurf, blue, (60, 120), (120, 120), 4)

# 绘制一个圆
pygame.draw.circle(displaysurf, blue, (300, 50), 20, 0)

# 绘制一个椭圆
pygame.draw.ellipse(displaysurf, red, (300, 250, 40, 80), 1)

# 绘制一个矩形
pygame.draw.rect(displaysurf, red, (200, 150, 100, 50))

pixObj = pygame.PixelArray(displaysurf)
pixObj[480][380] = black
pixObj[482][382] = black
pixObj[484][384] = black
pixObj[486][386] = black
pixObj[488][388] = black

# 创建一个PixelArray对象，将会“锁定”Surface对象，锁定的时候不能绘制图片
# 删除PixelArray对象后，会“解锁”Surface对象
del pixObj

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # 让Surface对象显示到显示器上
    pygame.display.update()
